layui.define(["form","jquery"],function(exports){
    var form = layui.form,
    $ = layui.jquery,
    Address = {
        provinces : function(callingprovince,callingcity,calledprovince,calledcity,roamcity) {
            //加载省数据
            var callingproHtml = '',callingthat = this;
            var calledproHtml = '',calledthat = this;
            var roamproHtml = '',roamthat = this;
            $.get("../static/json/address.json", function (data) {
                for (var i = 0; i < data.length; i++) {
                	if(data[i].name == callingprovince){
                		callingproHtml += '<option value="' + data[i].name + '" selected>' + data[i].name + '</option>';
                	}else{
                		callingproHtml += '<option value="' + data[i].name + '">' + data[i].name + '</option>';
                	}
                	if(data[i].name == calledprovince){
                		calledproHtml += '<option value="' + data[i].name + '" selected>' + data[i].name + '</option>';
                	}else{
                		calledproHtml += '<option value="' + data[i].name + '">' + data[i].name + '</option>';
                	}
                	if(data[i].name == '湖北省'){
                		roamproHtml = '<option value="' + data[i].name + '" selected>' + data[i].name + '</option>';
                	}
                }
                //城市数据初始化
                for (var i = 0; i < data.length; i++) {
                	var dataName = data[i].name;
                	if(dataName == callingprovince){
                		var cityNameArray = data[i].childs;
                		callingthat.citys(cityNameArray,callingcity);
                	}
                }
                //城市数据初始化
                for (var i = 0; i < data.length; i++) {
                	var dataName = data[i].name;
                	if(dataName == calledprovince){
                		var cityNameArray = data[i].childs;
                		calledthat.calledcitys(cityNameArray,calledcity);
                	}
                }
                //初始化省数据
                $("select[name=callingProvince]").append(callingproHtml);
                $("select[name=calledProvince]").append(calledproHtml);
                $("select[name=roamProvince]").append(roamproHtml);
                form.render('select');
                form.on('select(callingProvince)', function (proData) {
                    var value = proData.value;
                    if (value != '' && value != null) {
                    	callingthat.citys(data[$(this).index() - 1].childs);
                    } else {
                        $("select[name=callingCity]").attr("disabled", "disabled");
                    }
                });
                form.on('select(calledProvince)', function (proData) {
                    var value = proData.value;
                    if (value != '' && value != null) {
                    	calledthat.calledcitys(data[$(this).index() - 1].childs);
                    } else {
                        $("select[name=calledCity]").attr("disabled", "disabled");
                    }
                });
                roamthat.roamcitys(data[16].childs,roamcity);
            })
        },
        //加载市数据
        citys : function(citys,city) {
            var cityHtml = '<option value="">请选择市</option>',that = this;
            for (var i = 0; i < citys.length; i++) {
            	if(citys[i].name == city){
            		cityHtml += '<option value="' + citys[i].name + '" selected>' + citys[i].name + '</option>';
            	}else{
            		cityHtml += '<option value="' + citys[i].name + '">' + citys[i].name + '</option>';
            	}
            }
            $("select[name=callingCity]").html(cityHtml).removeAttr("disabled");
            form.render('select');
        },
        //加载市数据
        calledcitys : function(citys,city) {
            var cityHtml = '<option value="">请选择市</option>',that = this;
            for (var i = 0; i < citys.length; i++) {
            	if(citys[i].name == city){
            		cityHtml += '<option value="' + citys[i].name + '" selected>' + citys[i].name + '</option>';
            	}else{
            		cityHtml += '<option value="' + citys[i].name + '">' + citys[i].name + '</option>';
            	}
            }
            $("select[name=calledCity]").html(cityHtml).removeAttr("disabled");
            form.render('select');
        },
        //加载市数据
        roamcitys : function(citys,city) {
            var cityHtml = '<option value="">请选择市</option>',that = this;
            for (var i = 0; i < citys.length; i++) {
            	if(citys[i].name == city){
            		cityHtml += '<option value="' + citys[i].name + '" selected>' + citys[i].name + '</option>';
            	}else{
            		cityHtml += '<option value="' + citys[i].name + '">' + citys[i].name + '</option>';
            	}
            }
            $("select[name=roamCity]").html(cityHtml).removeAttr("disabled");
            form.render('select');
        }
    };
    exports("address",Address);
})